<template>
  <div>
    <el-form ref="unvipform" :model="vipData" :rules="vipRules" label-width="150px">
      <el-form-item label="会员等级名称" prop="name">
        <el-input v-model="vipData.name" placeholder="会员等级名称" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="等级积分" prop="score"> 
        <el-input v-model="vipData.min_score" placeholder="请输入等级积分" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="优惠比例(%)" prop="ratio"> 
        <el-input v-model="vipData.discount_ratio" placeholder="请输入优惠比例" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="每月赠送优惠券数量" prop="count"> 
        <el-input v-model="vipData.voucher_count" placeholder="请输入赠送优惠券数量" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="代金券类型">
        <el-select v-model="vipData.voucher_type_id" placeholder="请选择代金券类型" size="small" style="max-width:70">
          <el-option :label="item.name" :value="item.id" v-for="item in shopVouList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="会员获取积分的倍数" prop="times">
        <el-input v-model="vipData.score_times" placeholder="请输入购买次数" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="vipData.status" active-color="#13ce66"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import store from '@/store'
import { postUnPayVip, putUnPayVip, getShopVouType } from '@/api/vip'

export default {
  name: 'vipPanel',
  props: {
    isAdd: {// 是否是新增
      type: Boolean,
      required: true,
      default: true
    },
    vipData: {
      type: Object,
      default: () => {
        return {
          key: store.state.app.activeApp.saa_key,
          name: '',
          min_score: '',
          discount_ratio: '',
          voucher_type_id: '',
          score_times: '',
          status: true
        }
      }
    }
  },
  data() {
    return {
      vipRules: {
        name: [
          { required: false, message: '请输入会员卡名称', trigger: 'blur' },
        ],
        score: [
          { required: false, message: '请输入等级积分', trigger: 'blur' },
        ],
        ratio: [
          { required: false, message: '请输入优惠比例', trigger: 'blur' },
        ],
        count: [
          { required: false, message: '请输入赠送优惠券数量', trigger: 'blur' },
        ],
        times: [
          { required: false, message: '请输入积分倍数', trigger: 'blur' },
        ]
      },
      shopVouList: [],
    }
  },
  watch: {
    shopVouList(newData) {
      if(newData.length>0){
        this.vipData.voucher_type_id = newData[0].id;
      }
    }
  },
  mounted() {
    this.getShopVouType()
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.unvipform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            this.vipData.status = this.vipData.status ? '1' : '0';
            postUnPayVip(this.vipData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            const params = {
              key: this.$store.state.app.activeApp.saa_key,
              id: this.vipData.id,
              name: this.vipData.name,
              discount_ratio: this.vipData.discount_ratio,
              voucher_count: this.vipData.voucher_count,
              voucher_type_id: this.vipData.voucher_type_id,
              score_times: this.vipData.score_times,
              status: this.vipData.status ? '1' : '0',
            }
            putUnPayVip(params).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })
      
    },
    /**
     * 获取代金券类型
     */
    getShopVouType() {
      getShopVouType({ key: this.$store.state.app.activeApp.saa_key, type: '1' }).then(response => {
        if(response.status === 200){
          this.shopVouList = response.data;
        }else{
          this.$message.error(response.message);
        }
      })
    }
  },
}
</script>